package com.qymerge.manage.system.dao.impl;

import com.qymerge.manage.base.ro.MetaOrgVersionRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.dao.BaseDao;
import com.qymerge.manage.system.dao.ISysUserLoginDao;
import com.qymerge.manage.system.ro.SysUserLogin;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 *
 * @author Xiao
 * @version 1.0.0
 * @date 2025/6/23 22:07
 */
@Repository
public class SysUserLoginDaoImpl extends BaseDao implements ISysUserLoginDao {

    private String dwd_MCRS_MDM_sys_user_login = "SELECT id,user_phone,login_time,create_user_id,create_time,update_user_id,update_time,status FROM dwd_MCRS_MDM_sys_user_login WHERE 1=1 ";

    @Override
    public List<SysUserLogin> list(SysUserLogin ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = dwd_MCRS_MDM_sys_user_login;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getUser_phone())) {
            sql += " and user_phone=:user_phone";
            paramMap.put("user_phone", ro.getUser_phone());
        }

        // 分页数据获取
        sql += " order by Id desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<SysUserLogin>(SysUserLogin.class));
    }

    @Override
    public int total(SysUserLogin ro) {
        String sql = "select count(1) from dwd_MCRS_MDM_sys_user_login where 1=1 ";
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getUser_phone())) {
            sql += " and user_phone=:user_phone";
            paramMap.put("user_phone", ro.getUser_phone());
        }

        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public List<SysUserLogin> findAllByUserId(Long uid) {
        return Collections.emptyList();
    }

    @Override
    public int install(SysUserLogin ro) {
        StringBuilder sql = new StringBuilder("insert into dwd_MCRS_MDM_sys_user_login(user_phone, status) values(?,?)");

        List params = new ArrayList();
        params.add(ro.getUser_phone());
        params.add(ro.getStatus());

        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }


}
